$(function () {	
	   //传入数据长度 计算时间  获取时间
		//var dataLen=10;
	   function getTime(dataLen){
		    var timesTamp = '';
			var curTime=new Date();
			var ts=curTime.getTime(); //改为时间戳
			//ts -= 10 * 60 * 1000;
			//alert(ts);
			for (var i = 0; i < dataLen; i++) {
				ts -= 1 * 60 * 1000;  //相差dataLen分钟 每次减一分钟的时间戳
				//alert(ts);
				var utc= new Date(ts);
				var curHour = utc.getHours(); 
				var curMinute = utc.getMinutes();
				if(curHour < 10){
					curHour = "0" + curHour;
				}
				if(curMinute < 10){
					curMinute = "0" + curMinute;
				}
				curTime = curHour + ':' + curMinute;
				var timesTampTemp = "\""+  curTime  +"\""+ ",   ";
				timesTamp = timesTampTemp + timesTamp  ;
			}
			return timesTamp;
			//alert(timesTamp);
	   }
	   //getTime(8);
	   
	   
	   
	   function getClusterGraph(){
		   //++++++++++++++++++++++++++++++++//
		   //cpu折线图  
		    var cpu_len = 0;//得到一条线里面有几个数据数目 用于计算时间点
		    var cpu_json = {};
		    var cpu_chart = {
  		       type: 'spline'      
  		    }; 
  		    var cpu_title = {
  		       text: '<b>CPU利用率<b>'   
  		    };
  		    var cpu_subtitle = {
  		       text: '每一分钟更新一次'
  		    };
  		    var cpu_yAxis = {
  		       title: {
  		           text: ' 利用率 '
  		       },
  		       plotLines: [{
  		          value: 0,
  		          width: 1,
  		          color: '#808080'
  		       }]
  		    };   
		    
			$.ajax({
				type:"get",
		        dataType:"json",
		        cache:false,
		        async:false,
		        url:"/detection/cpu/node/utilization/", 
		        success: function (data) {
		        	//修改json格式，改为画图可以用的
		        	for(var i = 0; i < data.length; i++) {
		        		 data[i]["name"] = data[i].nodename;   //'name'是需要的字段
		                 delete data[i].nodename;  //nodename是要替换为'name'的字段
		                 data[i]["data"] = data[i].values;
		                 delete data[i].value;
		                 //len取最大的长度
		                 if(cpu_len < data[i].data.length){
		                	 cpu_len = data[i].data.length;
		                 }
	                }
		           //alert(len);
		           
	      		   //提示信息
	      		   var cpu_tooltip = {
	      			  crosshairs: true,
	      		      shared: true,
	      		   };
	      		   //展示方式右对齐
	      		   var cpu_legend = {
	      		      layout: 'vertical',
	      		      align: 'right', //图例位置
	      		      verticalAlign: 'middle',
	      		      borderWidth: 0
	      		   };
		           var  cpu_time = getTime(cpu_len);
		           //alert(thisTime);
		           var cpu_xAxis = {
	      		      categories: eval("[" + cpu_time + "]")
	      		   };
		        	
			       //cpu 折线图
				   var cpu_series =  data;
				
				   cpu_json.xAxis = cpu_xAxis;
				   cpu_json.tooltip = cpu_tooltip;
				   cpu_json.series = cpu_series;
				  
		    	
		        }//sucess
		
			});//ajax
			cpu_json.title = cpu_title;
		    cpu_json.subtitle = cpu_subtitle;
		    cpu_json.yAxis = cpu_yAxis;
		    $('#cpu_container').highcharts(cpu_json);
			
			/*var cpu_chart = $('#cpu_container').highcharts();
			cpu_chart.reflow();*/
		    
			//++++++++++++++++++++++++++++++++//
		    //内存折线图数据
			var memory_len = 0;
			var memory_json = {};
		    var memory_title = {
		        text: '<b> 内存利用率 <b>'   
		    };
		    var memory_subtitle = {
		        text: '每分钟更新一次'
		    };
		    var memory_yAxis = {
		       title: {
		          text: '利用率'
		       },
		       plotLines: [{
		          value: 0,
		          width: 1,
		          color: '#808080'
		       }]
		    };   
			$.ajax({
				type:"get",
		        dataType:"json",
		        cache:false,
		        async:false,
		        url:"/detection/memory/node/utilization/", 
		        success: function (data) {
			    	for(var i = 0; i < data.length; i++) {
		        		 data[i]["name"] = data[i].nodename;   //'name'是需要的字段
		                 delete data[i].nodename;  //nodename是要替换为'name'的字段
		                 data[i]["data"] = data[i].values;   
		                 delete data[i].value;
		                 //len取最大的长度
		                 if(memory_len < data[i].data.length){
		                	 memory_len = data[i].data.length;
		                 }
	                }
			    	
			       //内存折线画图
				  
				   var memory_time = getTime(memory_len);
				   var memory_xAxis = {
					  categories: eval("[" + memory_time + "]")
							  
				   };
				   
				   //提示信息
				   var memory_tooltip = {
						   crosshairs: true,
						      shared: true,
					  
				   }
				   //展示方式右对齐
				   var memory_legend = {
				      layout: 'vertical',
				      align: 'right', //图例位置
				      verticalAlign: 'middle',
				      borderWidth: 0
				   };

				   var memory_series =  data;
				   //alert(memory_node[0]);
				   memory_json.xAxis = memory_xAxis;
				   memory_json.tooltip = memory_tooltip;
				   memory_json.series = memory_series;
				  
		        }//sucess
		
			});//ajax
			memory_json.title = memory_title;
		    memory_json.subtitle = memory_subtitle;
		    memory_json.yAxis = memory_yAxis;
		    $('#memory_container').highcharts(memory_json);
		    	 
		    /*var memory_chart = $('#memory_container').highcharts();
		    memory_chart.reflow();*/
			//++++++++++++++++++++++++++++++++//
		    //网络柱状图数据
			//定义二维数组  
        	
		
        	 //网络折线传数据
			var net_len = 0;
			var network_json = {};
			var network_chart = {
		       type: 'spline'      
		    }; 
		   var network_title = {
		       text: '<b>网络传输<b>'   
		    };
		   var network_subtitle = {
		       text: '每分钟更新一次'
		    };
        	$.ajax({
				type:"get",
		        dataType:"json",
		        cache:false,
		        async:false,
		        url:"/detection/network/node/rxtx/", 
		    
		        success: function (data) {
		        	
			    	for(var i = 0; i < data.length; i++) {
		        		 data[i]["name"] = data[i].nodename;   //'name'是需要的字段
		                 delete data[i].nodename;  //nodename是要替换为'name'的字段
		                 data[i]["data"] = data[i].values;   
		                 delete data[i].value;
		                 //len取最大的长度
		                 if(net_len < data[i].data.length){
		                	 net_len = data[i].data.length;
		                 }
		                 
	                }
			    	
			    	//alert(network_node[1]);
			       //网络折线画图
			       
				   var net_time = getTime(net_len);
				   var network_xAxis = {
					  categories: eval("[" + net_time + "]")
				   };
				   var network_yAxis = {
				      title: {
				         text: '传输速度 (Gb/s)'
					  },
					  plotLines: [{
					         value: 0,
					         width: 1,
					         color: '#808080'
					  }]
				   };
				   var network_tooltip = {
				      crosshairs: true,
				      shared: true
				   
				   };
				   var network_plotOptions = {
				      spline: {
				         marker: {
				            radius: 4,
				            lineColor: '#666666',
				            lineWidth: 1
				         }
				      }
				   };
				   var network_series= data; 
				   
				   network_json.tooltip = network_tooltip;
				   network_json.xAxis = network_xAxis;
				   network_json.yAxis = network_yAxis;  
				   network_json.series = network_series;
				   network_json.plotOptions = network_plotOptions;
				   

		        }//sucess
        	
			});//ajax
        	network_json.chart = network_chart;
		    network_json.title = network_title;
		    network_json.subtitle = network_subtitle;
		    $('#network_container').highcharts(network_json);
		    /*var network_chart = $('#network_container').highcharts();
		    network_chart.reflow();*/
		    
           //++++++++++++++++++++++++++++++++//
		   //storage_container存储折线图
        	var storage_len = 0;
        	 var storage_json = {};
			   
        	var storage_chart = {
		       type: 'spline'      
		    }; 
		    var storage_title = {
		       text: '<b>存储空间<b>'   
		    };
		    var storage_subtitle = {
		       text: '每分钟更新一次'
		    };
        	$.ajax({
				type:"get",
		        dataType:"json",
		        cache:false,
		        async:false,
		        url:"/detection/storage/node/free/", 
		    
		        success: function (data) {
		        	
			    	for(var i = 0; i < data.length; i++) {
		        		 data[i]["name"] = data[i].nodename;   //'name'是需要的字段
		                 delete data[i].nodename;  //nodename是要替换为'name'的字段
		                 data[i]["data"] = data[i].values;   
		                 delete data[i].value;
		                 //len取最大的长度
		                 if(storage_len < data[i].data.length){
		                	 storage_len = data[i].data.length;
		                 }
		                 
	                }
			    	
			    	//alert(network_node[1]);
			       //网络折线画图
			       
				   var storage_time = getTime(storage_len);
				   var storage_xAxis = {
				   	  crosshair: true,
				      categories: eval("[" + storage_time + "]")
				   };
				   
				   var storage_yAxis = {
				      title: {
				         text: '剩余存储空间 (Gb)'
					  },
					  plotLines: [{
					         value: 0,
					         width: 1,
					         color: '#808080'
					  }]
				   };
				   var storage_tooltip = {
				      crosshairs: true,
				      shared: true
				   
				   };
				   var storage_plotOptions = {
				      spline: {
				         marker: {
				            radius: 4,
				            lineColor: '#666666',
				            lineWidth: 1
				         }
				      }
				   };
				  
				   var storage_series= data;      
				   
				  
				   storage_json.tooltip = storage_tooltip;
				   storage_json.xAxis = storage_xAxis;
				   storage_json.yAxis = storage_yAxis;  
				   storage_json.series = storage_series;
				   storage_json.plotOptions = storage_plotOptions;
				  
		        }//sucess
        	
			});//ajax
        	storage_json.chart = storage_chart;
		    storage_json.title = storage_title;
		    storage_json.subtitle = storage_subtitle;
		    $('#storage_container').highcharts(storage_json);
		    /*var storage_chart = $('#storage_container').highcharts();
		    storage_chart.reflow();*/
        	
        	
	   }//graph
	   
	   getClusterGraph();
	   setInterval(getClusterGraph,60000);
	 
	   function getClusterTable(){
		    
		    var row='';
		    var statusStyle=''
		    var statusValue=''
			$.ajax({
				type:"get",
				url: "/detection/podInfo/software/status/",
				cache:false,
				async:false,
				success: function(data){
				/*alert(JSON.stringify(data.length)); */ 
					//for(var i = 0; i < data.length; i++){    //获取后台传过来的url中的数据
					for(var i = 0; i < data.length-1; i++){    //获取后台传过来的url中的数据
			            var softwareName = data[i].softwareName;
			            if(softwareName=="neo4j"){
			            	softwareName="InGraphDB";
			            }else if(softwareName=="cassandra"){
			            	softwareName="KVStore";
			            }else if(softwareName=="iotdb"){
			            	softwareName="IotDB";
			            }else if(softwareName=="mysql"){
			            	softwareName="KingBase";
			            }else if(softwareName=="nosqldb"){
			            	softwareName="UnStructFS";
			            }
			            
			            var status = data[i].status;
			            if(status=="running"){
			            	statusValue=1;
			            }else if(status=="down"){
			            	statusValue=0; //为了检查 应该statusValue=0
			            }
			            
			            switch(statusValue){         //对违反规则的严重程度进行判断,并进行显式转换
			    		case 1:
			    			statusStyle = 'status-circle status-running';
			    			break;
			    		case 0:
			    			statusStyle = 'status-circle status-crash';
			    		 	break;
			    		
			    		default:
			    			statusStyle = 'status-metro status-crash';
			    		};
			            
			    		row += '<tr><td>' + softwareName + '</td><td><div class="'+statusStyle +'">'+'</div></td></tr>'
		    		
			       };
			       $('#cluster_table_body').html(row);//将每一行的数据加载到html中
			       $('#cluster_table').footable();//对footable进行初始化
				},
				error:function(jqXHR, textStatus, errorThrown){
	        	    $('#cluster_table').footable();//对footable进行初始化
					var cluster_pod_error = '<tr class="footable-empty"><td colspan="2">No results</td></tr>';
					var tabletTbody = $("#cluster_table").find('tbody');
	        	    tabletTbody.empty();
	        	    tabletTbody.append(cluster_pod_error);

				}
			});	
		
	   }
	   
	   getClusterTable();
	   setInterval(getClusterTable,60000);
   
   
   
	   
	   
	 
});


